import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

def send_email(msg_from, passwd, msg_to, text_content, file_path=None):
  msg = MIMEMultipart()
  subject = "邮件"  # 主题
  text = MIMEText(text_content)
  msg.attach(text)

  #file_path = r'test.txt'  #如果需要添加附件，就给定路径
  if file_path:  # 最开始的函数参数我默认设置了None ，想添加附件，自行更改一下就好
      docFile = file_path
      docApart = MIMEApplication(open(docFile, 'rb').read())
      docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
      msg.attach(docApart)
      print('发送附件！')
  msg['Subject'] = subject
  msg['From'] = msg_from
  msg['To'] = msg_to
  try:
      s = smtplib.SMTP_SSL("smtp.qq.com", 465)
      s.login(msg_from, passwd)
      s.sendmail(msg_from, msg_to, msg.as_string())
      print("发送成功")
  except smtplib.SMTPException as e:
      print("发送失败")
  finally:
      s.quit()

msg_from = '1984832368@qq.com'  # 发送方邮箱
passwd = 'pnrluixkccfrfcfh'  # 填入发送方邮箱的授权码（就是刚刚你拿到的那个授权码）
msg_to = '1984832368@qq.com'  # 收件人邮箱，我是自己发给自己
text_content = "test"  # 发送的邮件内容
file_path = 'test.txt'  # 需要发送的附件目录
send_email(msg_from, passwd, msg_to, text_content)
